//var menuTree;
var prefix = basepath + "/role"

var menuIds;
var enterpriseList;
var compPreviligeList;
var allinpayPreviligeList;
$(function() {
	loadAipInsList();
	getMenuTreeData();
	validateRule();
});
$.validator.setDefaults({
	submitHandler : function() {
		getAllSelectNodes();
		save();
	}
});

function getAllSelectNodes() {
	var ref = $('#menuTree').jstree(true); // 获得整个树

	menuIds = ref.get_selected(); // 获得所有选中节点的，返回值为数组

	$("#menuTree").find(".jstree-undetermined").each(function(i, element) {
		menuIds.push($(element).closest('.jstree-node').attr("id"));
	});
}
function loadAipInsList(){
	let selector = $('#aipIns')
	selector.empty()
	console.log('aipInsList:' + JSON.stringify(aipInsList))
	for (let i = 0; i < aipInsList.length; i++) {  
		selector.append("<option value=" + aipInsList[i].instanceId + ">" + aipInsList[i].enterpriseName + "</option>");  
    } 
}
function getMenuTreeData() {
	
	$.ajax({
		type : "GET",
		url : prefix+"/enterpriseList",
		success : function(data) {
			enterpriseList = data.dataList;
			// console.log('enterpriseList:' + JSON.stringify(data))
			let selector = $('#instanceId')
			selector.empty()
			selector.append("<option value='' disabled selected style='display:none;'>请选择</option>")
			selector.append("<option value=\"9999\">通联</option>")
			for (let i = 0; i < enterpriseList.length; i++) {  
				selector.append("<option value=" + enterpriseList[i].instanceId + ">" + enterpriseList[i].enterpriseName + "</option>");  
	        } 
//			selector.selectpicker('refresh');
//			selector.selectpicker('render');
		}
	});
	
	$.ajax({
		type : "GET",
		url : prefix + "/compPrivilegeList",
		success : function(data) {
			compPreviligeList = data;
			// console.log('compPreviligeList:' + JSON.stringify(data))
		}
	});
	
	$.ajax({
		type : "GET",
		url : prefix + "/allinpayPrivilege",
		success : function(data) {
			allinpayPreviligeList = data;
			// console.log('allinpayPreviligeList:' + JSON.stringify(data))
		}
	});
	
}

function reloadMenuTree() {
	let instanceId = $("#instanceId option:selected").val()
	if( instanceId === '9999'){
		clearMenuTree()
		loadMenuTree(allinpayPreviligeList)
	}else if( instanceId != '') {
		clearMenuTree()
		loadMenuTree(compPreviligeList)
	}else{
		clearMenuTree()
	}
}

function clearMenuTree() {
	let menuNode = document.getElementById("menuTree")
	let parentNode = menuNode.parentNode
	parentNode.removeChild(menuNode)
	var para=document.createElement("div");
	para.setAttribute("id", "menuTree")
	parentNode.appendChild(para)

}

function loadMenuTree(menuTree) {
	$('#menuTree').jstree({
		'core' : {
			'data' : menuTree
		},
		"checkbox" : {
			"three_state" : true,
		},
		"plugins" : [ "wholerow", "checkbox" ]
	});
	//$('#menuTree').jstree("open_all");

}

function save() {
	$('#menuIds').val(menuIds);
	var role = $('#signupForm').serialize();
	console.log('role:' + JSON.stringify(role) )
	$.ajax({
		cache : true,
		type : "POST",
		url : prefix + "/addRoleInfo",
		data : role, // 你的formid

		async : false,
		error : function(request) {
			alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
//				sleep(3000);
				parent.searchRole();
				var index = parent.layer.getFrameIndex(window.name);
				parent.layer.close(index);

			} else {
				parent.layer.msg(data.msg);
			}
		}
	});
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			roleName : {
				required : true
			}
		},
		messages : {
			roleName : {
				required : icon + "请输入角色名"
			}
		}
	});
}

/**刷新权限列表 */
function reLoadList(){
    var managerType=$("#managerType").val();
    var instanceId=$("#instanceId").val();
    if(managerType==3){
        $.ajax({
           type:"post",
		   url:"../adminLoginJson/reLoadList.action",
		   data:{"instanceId":instanceId}, 
		   async : false, 
		   success:function(data){
		     var result = eval("(" + data + ")");	     
		     if(result.style==1){
		        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
		     }else if(result.style==2){
		        $.fn.zTree.init($("#treeDemo"), setting, zNodesCom);
		     }else{
		        $("#error").show();
			    $("#error").text("系统异常！");
		     }
		  },
		  error:function(){
			$("#error").show();
			$("#error").text("系统错误！");
		  }	    
      }); 
    }
}

